package com.github.tonydeng.fmj.pool;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;

/**
 * Redis Ops 工具类
 *
 * @author 何航杰
 */
@Component
public class RedisOpsUtil {

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    /**
     * 按key,value逐个putHashMap到指定redisKey
     *
     * @param redisKey
     * @param mapKey
     * @param value
     */
    public void putHashMap(String redisKey, String mapKey, String value) {
        redisTemplate.opsForHash().put(redisKey, mapKey, value);
    }

    /**
     * 获取redisKey下HashMap某个key的值
     *
     * @param redisKey
     * @param mapKey
     * @return
     */
    public String getHashMapStringValue(String redisKey, String mapKey) {
        return redisTemplate.opsForHash().hasKey(redisKey, mapKey)
                ? (String) redisTemplate.opsForHash().get(redisKey, mapKey) : null;
    }

    /**
     * 获取redisKey下HashMap
     *
     * @param redisKey
     * @return
     */
    public Map<Object, Object> getHashMap(String redisKey) {
        return redisTemplate.opsForHash().entries(redisKey);
    }

    /**
     * 判断redisKey下HashMap某个key是否存在
     *
     * @param redisKey
     * @param mapKey
     * @return
     */
    public boolean hasHashMapKey(String redisKey, String mapKey) {
        return redisTemplate.opsForHash().hasKey(redisKey, redisKey);
    }

    /**
     * 判断redisKey是否存在
     *
     * @param redisKey
     * @return
     */
    public boolean haskey(String redisKey) {
        return redisTemplate.hasKey(redisKey);
    }

    /**
     * Set key-value到redis
     *
     * @param redisKey
     * @param value
     */
    public void setValue(String redisKey, String value) {
        redisTemplate.opsForValue().set(redisKey, value);
    }

    /**
     * Set key-value到redis
     *
     * @param redisKey
     * @param value
     */
    public void setValue(String redisKey, String value, Long sec) {
        redisTemplate.opsForValue().set(redisKey, value, sec, TimeUnit.SECONDS);
    }

    /**
     * Get value
     *
     * @param redisKey
     * @return
     */
    public String getValue(String redisKey) {
        return redisTemplate.hasKey(redisKey) ? (String) redisTemplate.opsForValue().get(redisKey) : null;
    }

    public Set<String> getAll(String redisKey) {
        return redisTemplate.keys(redisKey + "*");
    }

    /**
     * 设置时效
     *
     * @param redisKey
     * @param timeout
     */
    public void expire(String redisKey, long timeout) {
        redisTemplate.expire(redisKey, timeout, TimeUnit.MINUTES);
    }

    /**
     * 往队列加入数据
     *
     * @param key
     * @param value
     */
    public void push(String key, String value) {
        redisTemplate.opsForList().leftPush(key, value);
    }

    public void rightPush(String key, String value) {
        redisTemplate.opsForList().rightPush(key, value);
    }

    /***
     * 从队列取出数据
     *
     * @param key
     * @return
     */
    public String pop(String key) {
        return redisTemplate.opsForList().leftPop(key);
    }

    public String leftPop(String key) {
        return redisTemplate.opsForList().leftPop(key);
    }

    public Long getListSize(String rediskey) {
        return redisTemplate.opsForList().size(rediskey);
    }

    public void delete(String key) {
        redisTemplate.delete(key);
    }

    public void deleteKeys(String key) {
        Set<String> keys = redisTemplate.keys(key + "*");
        for (String k:keys
             ) {
            redisTemplate.delete(k);
        }
    }

    public void clean() {
        Set<String> keys = redisTemplate.keys("*");
        redisTemplate.delete(keys);
    }
}
